package com.karst.essentials.chapter05
{
	import flash.display.Shape;
	import flash.events.Event;
	
	import org.aswing.AsWingManager;
	import org.papervision3d.cameras.CameraType;
	import org.papervision3d.materials.BitmapFileMaterial;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.objects.primitives.Sphere;
	import org.papervision3d.view.BasicView;
	
	internal class DebugCameraExample extends BasicView
	{
		public function DebugCameraExample()
		{
			super( AsWingManager.getStage().stageWidth, AsWingManager.getStage().stageHeight,
				true, false, CameraType.DEBUG );
			createView() ;
			startRendering() ;
		}

		private var earthSphere : Sphere = null ;
		private var material : BitmapFileMaterial = null ;
		private function createView() : void
		{
			createBg() ;
			if( material == null )
				material = new BitmapFileMaterial( "earth.jpg", true ) ;
			if( earthSphere == null )
				earthSphere = new Sphere( material, 200, 10, 6 ) ;
			scene.addChild( earthSphere ) ;
			
			for(var i:uint = 0;i <  150; i++)
			{ 
				var plane:Plane = new Plane(new ColorMaterial(Math.random() * 0xFFFFFF), 50,50, 1,1);
				plane.material.doubleSided = true;
				scene.addChild(plane);
				plane.x = Math.random()*20000 - 10000;
				plane.y = Math.random()*1500 - 750;
				plane.z = Math.random()*20000 -10000;
				plane.localRotationY = Math.random() * 180 - 90;
			}
			
			camera.z = -2000;
			camera.focus = 20;
			camera.far = 20000;
			
		}

		private var bg : Shape = null ;
		private function createBg() : void
		{
			if( bg == null )
			{
				bg = new Shape() ;
				bg.graphics.beginFill( 0x000000 ) ;
				bg.graphics.drawRect( 0, 0, AsWingManager.getStage().width - 15,
					AsWingManager.getStage().height - 40 ) ;
				bg.graphics.endFill() ;
			}
			addChildAt( bg, 0 ) ;
		}
		
		override protected function onRenderTick(event:Event=null):void
		{
			super.onRenderTick( event ) ;
			if( earthSphere )
				earthSphere.localRotationY-- ;
		}
	}
}